package com.jic.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jic.data.constant.consist.ResponseCode;
import com.jic.data.exception.AppRuntimeException;
import com.jic.data.mapper.MemberRegisterStsMapper;
import com.jic.data.model.entity.MemberBaseInfo;
import com.jic.data.model.entity.MemberRegisterSts;
import com.jic.data.service.MemberRegisterStsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 接口实现类
 *
 * @author : 建投数据科技(山东)有限公司
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-09 10:12:20
 */
@Service
@Slf4j
public class MemberRegisterStsServiceImpl implements MemberRegisterStsService {

    @Resource
    private MemberRegisterStsMapper mapper;


    /**
     * 新增记录
     *
     * @param request 需要保存的入参实体类
     * @return int 是否保存成功
     */
    @Override
    public int save(MemberRegisterSts request) {
        int flag = mapper.insert(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "新增失败");
        }
    }


    /**
     * 保存或修改记录信息
     *
     * @param request 需要保存的入参实体类
     * @return int 是否保存或修改成功
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:09
     */
    @Override
    public int saveOrUpdate(MemberRegisterSts request) {
        log.info("保存或修改记录信息，入参 = {}", JSONObject.toJSONString(request));
        try {
            if (request == null) {
                throw new AppRuntimeException(ResponseCode.EXCEPTION, "入参为空");
            }
            if (request.getId() != null && request.getId() != 0) {
                log.info("保存或修改记录信息，修改，入参 = {}", JSONObject.toJSONString(request));
                int flag = mapper.updateByPrimaryKey(request);
                if (flag == 1) {
                    return 1;
                }
            } else {
                log.info("保存或修改记录信息，保存，入参 = {}", JSONObject.toJSONString(request));
                int flag = mapper.insertSelective(request);
                if (flag == 1) {
                    return 1;
                } else {
                    throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改失败");
                }
            }
        } catch (Exception e) {
            log.error("用户保存或修改失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "用户保存或修改失败");
        }
        return -1;
    }

    /**
     * 删除记录
     *
     * @param request 需要删除,含有主键的入参实体类
     * @return int 是否删除成功
     */
    @Override
    public int deleteByPrimaryKey(MemberRegisterSts request) {
        int flag = mapper.deleteByPrimaryKey(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "删除失败");
        }
    }


    /**
     * 逻辑删除记录
     *
     * @param request 含有主键的逻辑删除实体类
     * @return int 是否逻辑删除成功
     */
    @Override
    public int deleteByPrimaryKeyLogically(MemberRegisterSts request) {
        request.setDeleteFlag(1);
        int flag = mapper.updateByPrimaryKey(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "逻辑删除记录失败");
        }
    }


    /**
     * 修改
     *
     * @param request
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(MemberRegisterSts request) {
        try {
            log.info("修改记录");
            MemberRegisterSts param = new MemberRegisterSts();
            BeanUtils.copyProperties(request, param);
            int flag = mapper.updateByPrimaryKeySelective(param);
            if (flag == 1) {
                return 1;
            } else {
                throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
            }
        } catch (Exception e) {
            log.error("修改记录失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
        }

    }

    /**
     * 记录详情
     *
     * @param request 查询的入参实体类
     * @return 数据库查询到的实体类
     */
    @Override
    public MemberRegisterSts getByPrimaryKey(MemberRegisterSts request) {
        return mapper.selectByPrimaryKey(request);
    }


    /**
     * 分页查询
     *
     * @param request 分页查询入参实体类
     * @return 含有分页信息的实体类列表
     */
    @Override
    public PageInfo<MemberRegisterSts> listPageBySelective(MemberRegisterSts request, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<MemberRegisterSts> list = mapper.listPageBySelective(request);
        return new PageInfo(list);
    }
    /**
     * 分页查询
     *
     * @param request 分页查询入参实体类
     * @return 含有分页信息的实体类列表
     */
    @Override
    public List<MemberRegisterSts> listLatest60DaysTrend(MemberRegisterSts request, int pageNum, int pageSize) {
        PageHelper.startPage(1, 60);
        PageHelper.orderBy(" create_time desc");
        List<MemberRegisterSts> list = mapper.listLatest60DaysTrend(request);
        return list;
    }

    /**
     * 不分页查询
     *
     * @param request 不分页查询入参实体类
     * @return 数据库查到的实体类列表
     */
    @Override
    public List<MemberRegisterSts> listAllRecord(MemberRegisterSts request) {
        //分页处理
        request.setDeleteFlag(0);
        return mapper.selectBySelective(request);
    }
    /**
     * 不分页查询
     *
     * @param request 不分页查询入参实体类
     * @return 数据库查到的实体类列表
     */
    @Override
    public Map<String,Integer> listRegisterTime(MemberRegisterSts request) {
        request.setDeleteFlag(0);
        List<MemberRegisterSts> list = mapper.selectBySelective(request);
        Integer less2 = 0;
        Integer total3To6 = 0;
        Integer total6To9 = 0;
        Integer total9To12 = 0;
        Integer total12To15 = 0;
        Integer total15To18 = 0;
        Integer total18To21 = 0;
        Integer total21To24 = 0;
        Integer more24 = 0;



        for (MemberRegisterSts item : list) {
            Date registerDate = item.getRegisterDate();
            Date stsDate = item.getStsDate();
            Long registerMonth = (stsDate.getTime() - registerDate.getTime())/(1000*60*60*24*30);
            if (registerMonth<=3){
                less2 = less2 + item.getMemberNumber();
            }
            else if (3<=registerMonth  && registerMonth<=6){
                total3To6 = total3To6 + item.getMemberNumber();
            }
            else if (6<=registerMonth  && registerMonth<=9){
                total6To9 = total6To9 + item.getMemberNumber();
            }
            else if (9<=registerMonth  && registerMonth<=12){
                total9To12 = total9To12 + item.getMemberNumber();
            }
            else if (12<=registerMonth  && registerMonth<=15){
                total12To15 = total12To15 + item.getMemberNumber();
            }
            else if (15<=registerMonth  && registerMonth<=18){
                total15To18 = total15To18 + item.getMemberNumber();
            }
            else if (18<=registerMonth  && registerMonth<=21){
                total18To21 = total18To21 + item.getMemberNumber();
            }
            else if (21<=registerMonth  && registerMonth<=24){
                total21To24 = total21To24 + item.getMemberNumber();
            }
            else if (24<=registerMonth ){
                more24 = more24 + item.getMemberNumber();
            }
        }

        Map<String,Integer> map = new HashMap<>();
        map.put("less2",less2);
        map.put("total3To6",total3To6);
        map.put("total6To9",total6To9);
        map.put("total9To12",total9To12);
        map.put("total12To15",total12To15);
        map.put("total15To18",total15To18);
        map.put("total18To21",total18To21);
        map.put("total21To24",total21To24);
        map.put("more24",more24);

        return map;
    }


    /**
     * 不分页查询
     *
     * @param request 不分页查询入参实体类
     * @return 数据库查到的实体类列表
     * @author tzc
     * @date 2020-5-19 18:23:33
     */
    @Override
    public MemberRegisterSts getCountOfMemberNum(MemberRegisterSts request) {
        //分页处理
        request.setDeleteFlag(0);
        Integer count = 0;
        List<MemberRegisterSts> list = mapper.selectBySelective(request);
        for (MemberRegisterSts item :list) {
            count += item.getMemberNumber();
        }

        MemberRegisterSts memberRegisterSts = new MemberRegisterSts();
        memberRegisterSts.setMemberNumber(count);
        return memberRegisterSts;
    }

}
